<template>
  <div class="register-container">
    <form>
      <input type="text" v-model="username" placeholder="用户名">
      <input type="password" v-model="password" placeholder="密码">
      <button @click="register">注册</button>
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
		user:[],
      username: '',
      password: ''
    };
  },
  onLoad() {
  	this.getUser();
  },
  methods: {
	  async getUser(){
		  const res=await this.$myRequest({
			  url:'user'
		  })
		  console.log(res.data);
		  this.user=res.data
	  },
	  
   async register() {
		if(this.username!=""&&this.password!=""){
			const name=this.username;
			for(var i=0;i<this.user.length+1;i++){
				if(i==this.user.length){
					const res=await this.$myRequest({
						url:'user/register',
						method:'POST',
						data:{
							username:this.username,
							password:this.password
						}
					})
					console.log(res)
					uni.switchTab({
						url:'/pages/my/my'
					})
					break;
				}
				console.log(this.user[i].username);
				if(name==this.user[i].username){
					uni.showToast({
						title:'用户名已存在',
						icon:'error'
					})
					break;
				}
			}
		}
		else{
			uni.showToast({
				title:"用户名或密码不能为空",
				icon:'error'
			})
		}
		
    }
  }
};
</script>

<style>
	
input{
	margin-top: 30px;
	border: solid;
	
	border-radius: 6rpx;
	width: 300px;
	height: 30px;
}

form {
  display: flex;
  flex-direction: column;
  align-items: center;
}



button {
  margin-top: 30px;
  padding: 10px 20px;
  background-color: #007aff;
  color: #ffffff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

button:hover {
  background-color: #0055cc;
}
</style>
